package air2.totc.crypt;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * Implémentation concrète de l'algorithme DES
 * @author tmz
 *
 */
public class DESEncoder extends SymetricalEncoder 
{
	public static int keyLength = 64;
	public DESEncoder(byte cle[]) throws NoSuchAlgorithmException, NoSuchPaddingException
	{
		this.c = Cipher.getInstance(Encoder.DESString);
		// On construit la clé..
		this.sKey = new SecretKeySpec(Encoder.padKeyToLength(cle, keyLength), Encoder.DESString);
	}
	
	@Override
	public byte[] encrypt(byte[] bOutData) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException 
	{
		c.init(Cipher.ENCRYPT_MODE, this.sKey);	// Mode encryption, clé symétrique sKey
		return c.doFinal(bOutData);
	}

	@Override
	public byte[] decrypt(byte[] bInData) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		c.init(Cipher.DECRYPT_MODE, this.sKey); // Mode déchiffrage, clé symétrique sKey
		return c.doFinal(bInData);
	}
}
